package renshi.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import renshi.base.BaseDAO;
import renshi.model.Apply;
import renshi.model.Student;
import renshi.model.Tclass;
import renshi.model.Teacher;

public class AdminDAO extends BaseDAO {
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpSession session = request.getSession();
	String cname=(String)session.getAttribute("cname");
	public AdminDAO(Connection con) {
		super(con);
	}
	
   //����һ����ʦ
	public boolean add(Teacher teacher){		
		return true;
	}
	
	public boolean delete(int id){
		return true;
	}
	
	public List<Teacher> queryall(){
		List<Teacher> list = new ArrayList<Teacher>();
		//������ݿ�
		String sql = "select * from Teacher";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Teacher teacher=new Teacher();
				teacher.setTid(rs.getInt("tid"));
				teacher.setLoginname(rs.getString("loginname"));
				teacher.setTurename(rs.getString("turename"));
				teacher.setPwd(rs.getString("pwd"));
				list.add(teacher);
			}
			return list;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean update(Teacher teacher){
		return true;
	}
	
	public boolean login(String loginname, String pwd){
		String sql="select * from Teacher where loginname='"+loginname+"' and pwd='"+pwd+"' and role=2";
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				return true;
			}else{
				return false;
			}
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}
	}
	//-------------��ѯ���еĽ�ʦ��Ϣ
	public List<Teacher> teacherinf(){
		List<Teacher> teacherlist = new ArrayList<Teacher>();
		//������ݿ�
		String sql = "select * from Teacher where role=1";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Teacher teacher=new Teacher();
				teacher.setTid(rs.getInt("tid"));
				teacher.setLoginname(rs.getString("loginname"));
				teacher.setTurename(rs.getString("turename"));
				teacher.setPwd(rs.getString("pwd"));
				teacherlist.add(teacher);
				System.out.println(rs.getString("loginname"));
			}

			return teacherlist;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	//---------------�鿴���е�ѧ��
	public List<Student> studentinf(){
		List<Student> studentlist = new ArrayList<Student>();
		//������ݿ�
		String sql = "select * from Student";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Student student=new Student();
				student.setId(rs.getInt("id"));
				
				student.setLoginname(rs.getString("name"));
				student.setNum(rs.getString("num"));
				student.setCname(rs.getString("cname"));
				studentlist.add(student);
				System.out.println(rs.getString("cname"));
				System.out.println(rs.getString("name"));
				System.out.println(rs.getString("num"));
				
			}

			return studentlist;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	//------------�鿴����ѧ����Ϣ
	public Student sdetail(int num){
		
		//������ݿ�
		String sql = "select * from Student where id='"+num+"'";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				Student student=new Student();
				student.setId(rs.getInt("id"));
				student.setCname(rs.getString("cname"));
				student.setName(rs.getString("name"));
				student.setNum(rs.getString("num"));
				System.out.println("aaaaaaaaaanuaaa"+rs.getString("cname"));
				return student;
				
			}
			
			else return null;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	//------------�鿴������ʦ��Ϣ
	public Teacher tdetail(int num){
		
		//������ݿ�
		String sql = "select * from Teacher where tid='"+num+"'";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				Teacher teacher=new Teacher();
				teacher.setTid(rs.getInt("tid"));
				teacher.setLoginname(rs.getString("loginname"));
				teacher.setTurename(rs.getString("turename"));
				
				return teacher;
				
			}
			
			else return null;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	//--------------�鿴ѧ�����ʦ
public Tclass steacher(String cname){
		
	    String sql = "select * from Class where cname='"+cname+"'";
			try {
				stmt = con.createStatement();
				rs = stmt.executeQuery(sql);
				if(rs.next()){
					Tclass tclass=new Tclass();
					tclass.setTname(rs.getString("tname"));
					return tclass;
				}
				else
					return null;
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
		
			
		}

//--------------�鿴��ʦ�İ༶
public Tclass tclass(String tname){
		
	    String sql = "select * from Class where tname='"+tname+"'";
	    System.out.println("rs.nextǰ���studentDAO��tttttttttttt"+tname);
			try {
				
				stmt = con.createStatement();		
				rs = stmt.executeQuery(sql);
               	if(rs.next()){
                 	Tclass tclass=new Tclass();
					tclass.setCname(rs.getString("cname"));
					return tclass;
				}
				else
					return null;
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
		
			
		}

   //-------- �̼���ٵ�
public List<Apply> shortapply(){
	List<Apply> applylist = new ArrayList<Apply>();
	//������ݿ�
	String sql = "select * from Apply where days<3 or days=3";
	//������ݿ�
	try{
		stmt=con.createStatement();
		rs=stmt.executeQuery(sql);
		while(rs.next()){
			Apply apply=new Apply();
			apply.setAid(rs.getInt("aid"));
			
			apply.setLoginname(rs.getString("num"));
			apply.setBegintime(rs.getString("begintime"));
			apply.setEndtime(rs.getString("endtime"));
			apply.setDays(rs.getInt("days"));
			apply.setReason(rs.getString("reason"));
			apply.setRemark(rs.getString("remark"));
			apply.setStatus(rs.getInt("status"));
			applylist.add(apply);
			System.out.println(rs.getString("begintime"));

			
		}
     System.out.println("ddddddddddddddd");
		return applylist;
	}
	catch(SQLException e){
		e.printStackTrace();
		return null;
	}
}
      //--------------������ٵ�
public List<Apply> alongApply(){
	List<Apply> applylist = new ArrayList<Apply>();
	//������ݿ�
	String sql = "select * from Apply where days>3";
	//������ݿ�
	try{
		stmt=con.createStatement();
		rs=stmt.executeQuery(sql);
		while(rs.next()){
			Apply apply=new Apply();
			apply.setAid(rs.getInt("aid"));
			
			apply.setLoginname(rs.getString("num"));
			apply.setBegintime(rs.getString("begintime"));
			apply.setEndtime(rs.getString("endtime"));
			apply.setDays(rs.getInt("days"));
			apply.setReason(rs.getString("reason"));
			apply.setRemark(rs.getString("remark"));
			apply.setStatus(rs.getInt("status"));
			applylist.add(apply);
			System.out.println(rs.getString("begintime"));

			
		}
     System.out.println("ddddddddddddddd");
		return applylist;
	}
	catch(SQLException e){
		e.printStackTrace();
		return null;
	}
}
//------------�����ڲ�ѯ


	//---------------�鿴���е���ٵ�
	public List<Apply> allapply(){
		List<Apply> applylist = new ArrayList<Apply>();
		//������ݿ�
		String sql = "select * from Apply";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Apply apply=new Apply();
				apply.setAid(rs.getInt("aid"));
				
				apply.setLoginname(rs.getString("num"));
				apply.setBegintime(rs.getString("begintime"));
				apply.setEndtime(rs.getString("endtime"));
				apply.setDays(rs.getInt("days"));
				apply.setReason(rs.getString("reason"));
				apply.setRemark(rs.getString("remark"));
				apply.setStatus(rs.getInt("status"));
				applylist.add(apply);
				System.out.println(rs.getString("begintime"));

				
			}
         System.out.println("ddddddddddddddd");
			return applylist;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
//------------------��ѯδ����ĳ�����ٵ�
	public List<Apply> longApply(){
		List<Apply> applylist = new ArrayList<Apply>();
		//������ݿ�
		String sql = "select * from Apply where days>3 and status=1";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Apply apply=new Apply();
				apply.setAid(rs.getInt("aid"));
				
				apply.setLoginname(rs.getString("num"));
				apply.setBegintime(rs.getString("begintime"));
				apply.setEndtime(rs.getString("endtime"));
				apply.setDays(rs.getInt("days"));
				apply.setReason(rs.getString("reason"));
				apply.setRemark(rs.getString("remark"));
				apply.setStatus(rs.getInt("status"));
				applylist.add(apply);
				System.out.println(rs.getString("begintime"));

				
			}
         System.out.println("ddddddddddddddd");
			return applylist;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
	//------------------��ѯ��������������ٵ�
	public List<Apply> applyed(){
		List<Apply> applylist = new ArrayList<Apply>();
		//������ݿ�
		String sql = "select * from Apply where status!=1";
		//������ݿ�
		try{
			stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Apply apply=new Apply();
				apply.setAid(rs.getInt("aid"));
				
				apply.setLoginname(rs.getString("num"));
				apply.setBegintime(rs.getString("begintime"));
				apply.setEndtime(rs.getString("endtime"));
				apply.setDays(rs.getInt("days"));
				apply.setReason(rs.getString("reason"));
				apply.setRemark(rs.getString("remark"));
				apply.setStatus(rs.getInt("status"));
				applylist.add(apply);
				System.out.println(rs.getString("begintime"));

				
			}
         System.out.println("ddddddddddddddd");
			return applylist;
		}
		catch(SQLException e){
			e.printStackTrace();
			return null;
		}
	}
//------------------���ѧ��
	public boolean addStudent(String num,String name,String cname,String pwd){
    String sql = "insert into Student (num,name,cname,pwd) values ('"+num+"','"+name+"','"+cname+"','"+pwd+"')";
		
		try {
			stmt = con.createStatement();
			int flag = stmt.executeUpdate(sql);
			
			if(flag>0){
				
				return true;
			}
			else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
		}
		
		return true;
	}
	//-------------ɾ����ٵ�
	public boolean deleteA(int aid){
	    String sql = "delete Apply where aid='"+aid+"'";
			
			try {
				stmt = con.createStatement();
				int flag = stmt.executeUpdate(sql);
				
				if(flag>0){				
					return true;
				}
				else
					return false;
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
			}
			
			return true;
		}
	
	//-------------ɾ����ʦ
	public boolean deleteT(int tid){
	    String sql = "delete Teacher where tid='"+tid+"'";
			
			try {
				stmt = con.createStatement();
				int flag = stmt.executeUpdate(sql);
				
				if(flag>0){
					
					return true;
				}
				else
					return false;
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
			}
			
			return true;
		}
	//-------------ɾ��ѧ��
	public boolean deleteS(int id){
	    String sql = "delete Student where id='"+id+"'";
			
			try {
				stmt = con.createStatement();
				int flag = stmt.executeUpdate(sql);
				
				if(flag>0){
					
					return true;
				}
				else
					return false;
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
			}
			
			return true;
		}
}
